/**
 * This class represents a custom rss feed parser which is used to parse
 * rss feeds for all rss feeds except the events calendar module in the 
 * iWestern application. It is a subclass of RssFeedParser and uses 
 * methods / attributes from this class when parsing the rss feed.
 * @author Chirag Mistry
 */

package ca.uwo.android.mustangs;

import java.io.InputStream;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;
import ca.uwo.android.rss.RssFeed;
import ca.uwo.android.rss.RssItem;
import ca.uwo.android.rss.SaxRssParser;

public class MustangsPhotoParser extends SaxRssParser {

	/**
	 * Used to tag log messages from this class
	 */
	private static final String TAG_NAME = "MustangsPhotoParser";

	protected static final String MEDIA_NAMESPACE = "http://search.yahoo.com/mrss/";

	protected static final String XML_NAMESPACE = "http://www.w3.org/2005/Atom";

	protected static final String FEED = "feed";

	protected static final String ENTRY = "entry";

	protected static final String HREF = "href";

	protected static final String TYPE = "type";
	
	protected static final String HTML = "text/html";

	/**
	 * Constructor for the rss feed parser
	 * 
	 * @param rssFeedURL
	 *            - the url of the rss feed to parse
	 */
	public MustangsPhotoParser(String rssFeedURL) throws Exception {
		super(rssFeedURL);
	}

	/**
	 * This method parses the rss feed and returns an rss feed object containing
	 * all the feed items read in from the source
	 * 
	 * @return
	 */
	public RssFeed parse() throws Exception {

		// represents current item
		// being parsed
		final RssItem currentItem = new RssItem();

		// the root element
		RootElement root = new RootElement(XML_NAMESPACE, FEED);

//		Log.i(TAG_NAME, "Got root element:  " + root);

		// rss feed object to store all rss items
		final RssFeed rssItems = new RssFeed();

		// get the entry element from root
		Element entry = root.getChild(XML_NAMESPACE, ENTRY);

//		Log.i(TAG_NAME, "Got entry: " + entry);

		String rssFeedText;

		// get the input stream to the
		// rss feed
		InputStream is = this.getInputStream();

		// convert special characters in
		// feed
		rssFeedText = convertSpecialChars(is);

//		Log.i(TAG_NAME, "Rss feed text = " + rssFeedText);

		// parse and store the link for the video
		Element link = entry.getChild(XML_NAMESPACE, LINK);
		
//		Log.i(TAG_NAME, "Got link: " + link);
		
		// just need the url, which is an attribute of this element
		link.setStartElementListener(new StartElementListener() {

			@Override
			public void start(Attributes attributes) {
				String type = attributes.getValue(TYPE);
				String url = attributes.getValue(HREF);
//				Log.i(TAG_NAME, "Found url " + url + " and type " + type);
				if (type.equals(HTML)) {
					currentItem.setLink(url);
//					Log.i(TAG_NAME, "Set link to " + url);
					rssItems.add(currentItem.copy());
				}
			}
		});

		// parse the rss feed directly from the input stream using utf 8
		// encoding
		Xml.parse(rssFeedText, root.getContentHandler());

//		Log.i(TAG_NAME, "Finished parsing feed, the number of items is "
//				+ rssItems.size());

		return rssItems;
	}
}
